package com.example.springflowable.ask_for_leave.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.springflowable.ask_for_leave.service.AskLeaveTaskService;
import com.example.springflowable.service.TaskCommonService;
import org.flowable.common.engine.impl.identity.Authentication;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.engine.runtime.ProcessInstanceBuilder;
import org.flowable.task.api.Task;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Classname AskLeaveTaskServiceImpl
 * @Description TODO
 * @Date 2022/5/6 12:00
 * @Created by XiongXiong
 * @Author: X.I.O
 */
@Service
public class AskLeaveTaskServiceImpl implements AskLeaveTaskService {

    Logger logger= LoggerFactory.getLogger(AskLeaveTaskServiceImpl.class);

    @Autowired
    RuntimeService runtimeService;

    @Autowired
    TaskService taskService;

    @Autowired
    private TaskCommonService taskCommonService;

    @Override
    public void startTaskAskForLeave() {
        Authentication.setAuthenticatedUserId("1000");

        String staffId = "1000";
        HashMap<String, Object> map = new HashMap<>();
        map.put("uid", staffId);
        ProcessInstanceBuilder processInstanceBuilder=runtimeService.createProcessInstanceBuilder();
        processInstanceBuilder.businessKey("123456789");
        processInstanceBuilder.processDefinitionKey("ask_for_leave");
        processInstanceBuilder.name("我要请假了");
        processInstanceBuilder.variables(map);
        ProcessInstance processInstance=processInstanceBuilder.start();
      //  ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("ask_for_leave", map);
        Authentication.setAuthenticatedUserId(null);
        runtimeService.setVariable(processInstance.getId(), "name", "javaboy");
        runtimeService.setVariable(processInstance.getId(), "reason", "休息一下");
        runtimeService.setVariable(processInstance.getId(), "days", 10);
        logger.info("创建请假流程 processId：{}", processInstance.getId());
    }

    @Override
    public void submitToZuzhang() {
        String zuzhangId = "90";
        String staffId = "1000";
        //员工查找到自己的任务，然后提交给组长审批
        List<Task> list = taskService.createTaskQuery().taskAssignee(staffId).orderByTaskId().desc().list();
        logger.info("走我了====>"+list.size());
        for (Task task : list) {
            Map<String,Object> objectMap=runtimeService.getVariables(task.getProcessInstanceId());
            logger.info("任务 ID：{}；任务处理人：{}；任务是否挂起：{}", task.getId(), task.getAssignee(), task.isSuspended());


            Map<String, Object> map = new HashMap<>();
            //提交给组长的时候，需要指定组长的 id
            map.put("uid", zuzhangId);
            Authentication.setAuthenticatedUserId(zuzhangId);
            taskService.addComment(task.getId(),task.getProcessInstanceId(),"这是审批信息");

            taskService.complete(task.getId(), map);

            Authentication.setAuthenticatedUserId(null);
        }
    }


    @Override
    public void zuZhangApprove() {
        String zuzhangId = "90";
        String managerId="80";
        List<Task> list = taskService.createTaskQuery().taskAssignee(zuzhangId).orderByTaskId().desc().list();
        for (Task task : list) {
            logger.info("组长 {} 在审批 {} 任务", task.getAssignee(), task.getId());
            Map<String, Object> map = new HashMap<>();
            //组长审批的时候，如果是同意，需要指定经理的 id
            map.put("uid", managerId);
            map.put("checkResult", "通过");
            //System.out.println(JSONObject.toJSONString(task.getCaseVariables()));
            taskService.complete(task.getId(), map);
        }
    }

    @Override
    public void zuZhangReject() {
        String zuzhangId = "90";
        List<Task> list = taskService.createTaskQuery().taskAssignee(zuzhangId).orderByTaskId().desc().list();
        for (Task task : list) {
            logger.info("组长 {} 在审批 {} 任务", task.getAssignee(), task.getId());
            Map<String, Object> map = new HashMap<>();
            //组长审批的时候，如果是拒绝，就不需要指定经理的 id
            map.put("checkResult", "拒绝");
            taskService.complete(task.getId(), map);
        }
    }

    @Override
    public void managerApprove() {
        String managerId="80";
        List<Task> list = taskService.createTaskQuery().taskAssignee(managerId).orderByTaskId().desc().list();
        for (Task task : list) {
            logger.info("经理 {} 在审批 {} 任务", task.getAssignee(), task.getId());
            Map<String, Object> map = new HashMap<>();
            map.put("checkResult", "通过");
            taskService.complete(task.getId(), map);
        }
    }

    @Override
    public void managerReject() {
        String managerId="80";
        List<Task> list = taskService.createTaskQuery().taskAssignee(managerId).orderByTaskId().desc().list();
        for (Task task : list) {
            logger.info("经理 {} 在审批 {} 任务", task.getAssignee(), task.getId());
            Map<String, Object> map = new HashMap<>();
            map.put("checkResult", "驳回");
            taskService.complete(task.getId(), map);
        }
    }
}
